﻿using SuperMap.UI;
using System;
using System.IO;
using System.Windows.Forms;
using Ycjw.PipeLine.Map.Basic.SuperMap;

namespace Ycjw.PipeLine.Map.Basic.Conditions
{
    public class BasicToolBar : Conditions.ConditionPanel
    {
        /// <summary>
        /// 对应的SceneOperInterface对象，方便统一管理地图注册事件
        /// </summary>
        private SceneOperInterface m_SceneOperInterface;
        /// <summary>
        /// 工具条关闭按钮
        /// </summary>
        public Sunny.UI.UISymbolButton uiSymbolButtonClose;
        /// <summary>
        /// 工具条ToolStrip对象
        /// </summary>
        private ToolStrip m_ToolStrip;

        /// <summary>
        /// 工具条ToolStrip对象
        /// </summary>
        public ToolStrip ToolStrip
        {
            get
            {
                return m_ToolStrip;
            }
        }
        /// <summary>
        /// 释放资源时统一释放对应的SceneOperInterface对象
        /// </summary>
        /// <param name="disposing"></param>
        protected override void Dispose(bool disposing)
        {
            if (SceneOperInterface != null)
            {
                SceneOperInterface.UnRegister();
                SceneOperInterface.Remove(SceneOperInterface.GetType());
            }

            base.Dispose(disposing);
        }
        public SceneOperInterface SceneOperInterface { get { return m_SceneOperInterface; } set { this.m_SceneOperInterface = value; } }
        /// <summary>
        /// 默认的漫游按钮
        /// </summary>
        private ToolStripButton toolStripButtonPan;
        /// <summary>
        /// 默认的选择按钮
        /// </summary>
        private ToolStripButton toolStripButtonSelect;
        /// <summary>
        /// 构造函数
        /// </summary>
        public BasicToolBar()
        {
            InitializeComponent();

            this.m_ToolStrip.ItemAdded += ToolStrip_ItemAdded;
            this.ParentChanged += BasicToolBar_ParentChanged;
            var img1 = Commons.Paths.ApplicationResourcesDir + "\\漫游.png";
            var img2 = Commons.Paths.ApplicationResourcesDir + "\\选择.png";
            if (System.IO.File.Exists(img1))
                this.toolStripButtonPan.Image = System.Drawing.Image.FromFile(img1);
            if (System.IO.File.Exists(img2))
                this.toolStripButtonSelect.Image = System.Drawing.Image.FromFile(img2);

            this.toolStripButtonPan.Click += ToolStripButtonPan_Click;
            this.toolStripButtonSelect.Click += ToolStripButtonSelect_Click;
            this.uiSymbolButtonClose.Click += UiSymbolButtonClose_Click;

            this.ParentChanged += BasicToolBar_ParentChanged;
        }
        /// <summary>
        /// 关闭父窗口时关闭对应的结果窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BasicToolBar_ParentChanged(object sender, EventArgs e)
        {
            if(this.Parent == null)
            {
                if(this.m_ResultWindow != null)
                {
                    this.m_ResultWindow.Close();
                }
            }
        }
        /// <summary>
        /// 点击关闭按钮关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UiSymbolButtonClose_Click(object sender, EventArgs e)
        {
            this.Parent.Controls.Remove(this);
            AfterClose();
            this.m_ResultWindow?.Close();
        }
        /// <summary>
        /// 添加菜单时自动配置图标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolStrip_ItemAdded(object sender, ToolStripItemEventArgs e)
        {
            e.Item.Click += Item_Click;
            if(e.Item.Image == null)
            {
                string path = Commons.Paths.ApplicationResourcesDir + "\\ToolBar\\" + e.Item.Text + ".png";
                if (!File.Exists(path))
                    path = Commons.Paths.ApplicationResourcesDir + "\\" + e.Item.Text + ".png";
                if (!File.Exists(path)) return;
                var img = System.Drawing.Image.FromFile(path);

                e.Item.Image = img;
            }
        }
        /// <summary>
        /// 点击每个按钮前取消注册的其他事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Item_Click(object sender, EventArgs e)
        {
            foreach (var soi in SceneOperInterface.AllSceneOperInterface.Values)
            {
                soi.UnRegister();
            }
        }
        /// <summary>
        /// 选中按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolStripButtonSelect_Click(object sender, EventArgs e)
        {
            SceneControl.Action = Action3D.Select;
        }
        /// <summary>
        /// 漫游按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolStripButtonPan_Click(object sender, EventArgs e)
        {
            SceneControl.Action = Action3D.Pan;
        }

        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(BasicToolBar));
            this.m_ToolStrip = new System.Windows.Forms.ToolStrip();
            this.toolStripButtonPan = new System.Windows.Forms.ToolStripButton();
            this.toolStripButtonSelect = new System.Windows.Forms.ToolStripButton();
            this.uiSymbolButtonClose = new Sunny.UI.UISymbolButton();
            this.m_ToolStrip.SuspendLayout();
            this.SuspendLayout();
            // 
            // m_ToolStrip
            // 
            this.m_ToolStrip.Dock = System.Windows.Forms.DockStyle.Fill;
            this.m_ToolStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolStripButtonPan,
            this.toolStripButtonSelect});
            this.m_ToolStrip.Location = new System.Drawing.Point(0, 0);
            this.m_ToolStrip.Name = "m_ToolStrip";
            this.m_ToolStrip.Padding = new System.Windows.Forms.Padding(0);
            this.m_ToolStrip.Size = new System.Drawing.Size(591, 28);
            this.m_ToolStrip.TabIndex = 0;
            this.m_ToolStrip.Text = "toolStrip";
            // 
            // toolStripButtonPan
            // 
            this.toolStripButtonPan.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButtonPan.Image")));
            this.toolStripButtonPan.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.toolStripButtonPan.Name = "toolStripButtonPan";
            this.toolStripButtonPan.Size = new System.Drawing.Size(52, 25);
            this.toolStripButtonPan.Text = "漫游";
            this.toolStripButtonPan.ToolTipText = "漫游";
            // 
            // toolStripButtonSelect
            // 
            this.toolStripButtonSelect.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButtonSelect.Image")));
            this.toolStripButtonSelect.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.toolStripButtonSelect.Name = "toolStripButtonSelect";
            this.toolStripButtonSelect.Size = new System.Drawing.Size(52, 25);
            this.toolStripButtonSelect.Text = "选择";
            // 
            // uiSymbolButtonClose
            // 
            this.uiSymbolButtonClose.Cursor = System.Windows.Forms.Cursors.Hand;
            this.uiSymbolButtonClose.Dock = System.Windows.Forms.DockStyle.Right;
            this.uiSymbolButtonClose.FillColor = System.Drawing.Color.FromArgb(((int)(((byte)(140)))), ((int)(((byte)(140)))), ((int)(((byte)(140)))));
            this.uiSymbolButtonClose.FillHoverColor = System.Drawing.Color.FromArgb(((int)(((byte)(158)))), ((int)(((byte)(160)))), ((int)(((byte)(165)))));
            this.uiSymbolButtonClose.FillPressColor = System.Drawing.Color.FromArgb(((int)(((byte)(121)))), ((int)(((byte)(123)))), ((int)(((byte)(129)))));
            this.uiSymbolButtonClose.FillSelectedColor = System.Drawing.Color.FromArgb(((int)(((byte)(121)))), ((int)(((byte)(123)))), ((int)(((byte)(129)))));
            this.uiSymbolButtonClose.Font = new System.Drawing.Font("微软雅黑 Light", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.uiSymbolButtonClose.Location = new System.Drawing.Point(563, 0);
            this.uiSymbolButtonClose.Margin = new System.Windows.Forms.Padding(5);
            this.uiSymbolButtonClose.MinimumSize = new System.Drawing.Size(1, 1);
            this.uiSymbolButtonClose.Name = "uiSymbolButtonClose";
            this.uiSymbolButtonClose.Radius = 10;
            this.uiSymbolButtonClose.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(140)))), ((int)(((byte)(140)))), ((int)(((byte)(140)))));
            this.uiSymbolButtonClose.RectHoverColor = System.Drawing.Color.FromArgb(((int)(((byte)(158)))), ((int)(((byte)(160)))), ((int)(((byte)(165)))));
            this.uiSymbolButtonClose.RectPressColor = System.Drawing.Color.FromArgb(((int)(((byte)(121)))), ((int)(((byte)(123)))), ((int)(((byte)(129)))));
            this.uiSymbolButtonClose.RectSelectedColor = System.Drawing.Color.FromArgb(((int)(((byte)(121)))), ((int)(((byte)(123)))), ((int)(((byte)(129)))));
            this.uiSymbolButtonClose.Size = new System.Drawing.Size(28, 28);
            this.uiSymbolButtonClose.Style = Sunny.UI.UIStyle.Gray;
            this.uiSymbolButtonClose.Symbol = 61453;
            this.uiSymbolButtonClose.SymbolSize = 16;
            this.uiSymbolButtonClose.TabIndex = 1;
            this.uiSymbolButtonClose.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // BasicToolBar
            // 
            this.Controls.Add(this.uiSymbolButtonClose);
            this.Controls.Add(this.m_ToolStrip);
            this.FillColor = System.Drawing.Color.FromArgb(((int)(((byte)(242)))), ((int)(((byte)(242)))), ((int)(((byte)(244)))));
            this.Name = "BasicToolBar";
            this.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(140)))), ((int)(((byte)(140)))), ((int)(((byte)(140)))));
            this.Size = new System.Drawing.Size(591, 28);
            this.Style = Sunny.UI.UIStyle.Gray;
            this.m_ToolStrip.ResumeLayout(false);
            this.m_ToolStrip.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }
    }
}
